package com.medicine.hbp.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

import com.medicine.hbp.R;
import com.medicine.hbp.adapter.FragmentAdapter;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.fragment.SearchFragment;
import com.medicine.hbp.fragment.x.Fragment01;
import com.medicine.hbp.fragment.x.Fragment02;
import com.medicine.hbp.fragment.x.Fragment03;
import com.medicine.hbp.fragment.x.Fragment04;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.QxInfoVo;
import com.medicine.hbp.page.ViewPageIndex;
import com.medicine.hbp.page.ViewPageIndex.OnItemPageChangeListener;
import com.medicine.hbp.sp.InterestSp;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.StartIntent;
import com.medicine.hbp.uitl.ToastUtil;

public class MenuActivity extends BaseActivity {
	@BindView(id = R.id.view_page, click = false)
	public ViewPager mViewPage;
	@BindView(id = R.id.indicator, click = false)
	public ViewPageIndex indicator;
	@BindView(id = R.id.img_bq, click = true)
	public ImageView img_bq;
	private List<String> title=Arrays.asList("推荐","专题","经验","收藏","搜索");
	//	private List<String> title=Arrays.asList("推荐","排名","热点","分类","收藏","搜索");
	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_menu);

	}
	private int arg=3;
	@Override
	public void initWidget() {
		super.initWidget();
		List<Fragment> listf = new ArrayList<Fragment>();
		Fragment fragment01= new Fragment01();
		listf.add(fragment01);
		Fragment fragment02= new Fragment02();
		listf.add(fragment02);
		Fragment fragment03= new Fragment03();
		listf.add(fragment03);
		Fragment fragment04= new Fragment04();
		listf.add(fragment04);
		Fragment fragmentSearch=new SearchFragment();
		listf.add(fragmentSearch);

		FragmentAdapter adapter=new FragmentAdapter(this.getSupportFragmentManager(), listf);
		mViewPage.setAdapter(adapter);
		mViewPage.setOffscreenPageLimit(1); //mViewPage缓存页面数量
		indicator.setNum(listf.size());
		indicator.setTabItemTitle(title,img_bq.getWidth(),1);
		indicator.setViewPageItem(mViewPage);

		indicator.setOnPageChangeListener(new OnItemPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				if(arg0==3){
					mViewPage.setCurrentItem(arg);
					if(TextUtils.isEmpty(LoginSp.getToken(aty))){
						StartIntent.startLogin(aty);
					}
				}else{
					arg=3;
				}

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
	}
	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		super.widgetClick(v);
		switch (v.getId()) {
			case R.id.img_bq:
				Intent intent=new Intent(aty,TheDiseaseTypesActivity.class);
				startActivity(intent);
				break;
			default:
				break;
		}
	}
	//	private void initTop() {
//		TopView topview=(TopView) findViewById(R.id.topview);
//		topview.setTitle("高血压应用");
//		topview.setLeftOnClick(new OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				if(LoginSp.getUserId(aty)!=0){
//					StartIntent.startUser(aty,LoginSp.getUserId(aty));
//				}else{
//					StartIntent.startLogin(aty);
//				}
//
//			}
//		});
//		topview.setRightOnClick(new OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				StartIntent.startRelease(aty);
//			}
//		});
//	}
//	/**
//	 * 权限接口
//	 */
//	private void httpqx() {
//		HttpParams params = new HttpParams();
//		AppHttps.HttpPost(this, HttpInterface.GET_APP_AUTH, params,
//				new AppHttpCallBack<QxInfoVo>(QxInfoVo.class,aty) {
//					@Override
//					public void onSuccess(String t) {
//						super.onSuccess(t);
//					}
//
//					@Override
//					public void onSuccess(QxInfoVo result) {
//						try{
//							if(result.info.authIsHomeClassify.equals("Y")){
//								InterestSp.setInterestYorN("Y", aty);
//								Intent intent=new Intent(aty,InterestActivity.class);
//								startActivity(intent);
//							}else{
//								InterestSp.setInterestYorN("N", aty);
//							}
//						}catch(Exception e){
//						}
//						
//					}
//					@Override
//					public void onFailures(int errorCode, String errorMsg) {
//						ToastUtil.showShort(aty, errorMsg);
//					}
//				});
//	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
}
